from __future__ import annotations

from dataclasses import dataclass
from math import tan, radians


@dataclass
class AnchorageResult:
    anchor_force_kN: float
    required_bearing_area_m2: float
    embed_depth_m: float
    note: str


def design_ground_anchor(
    main_rope_tension_kN: float,
    slope_deg: float,
    soil_allow_bearing_kPa: float,
    cohesion_kPa: float = 0.0,
    safety_factor: float = 2.0,
) -> AnchorageResult:
    """地锚简化设计：承压面积与埋深估算。

    基本假定：
    - 地表倾角 slope_deg
    - 地基承载力特征值 soil_allow_bearing_kPa
    - 需要考虑安全系数
    计算：
    - 合力沿地表法向分量作为承压设计值
    - A_req = (设计力) / (承载力设计值)
    - 埋深经验估算 h ≈ 0.5~1.5 m，随拉力与土体黏聚调整
    """

    theta = radians(slope_deg)
    design_force = main_rope_tension_kN * (1.0 + tan(theta) * 0.1)
    design_force *= safety_factor
    qd = soil_allow_bearing_kPa  # kN/m2
    area = max(0.1, design_force * 1000.0 / max(1e-6, qd)) / 1000.0  # 转为 m²
    depth = 0.8 + 0.002 * main_rope_tension_kN - 0.0005 * cohesion_kPa
    depth = max(0.6, min(2.5, depth))
    return AnchorageResult(
        anchor_force_kN=design_force,
        required_bearing_area_m2=area,
        embed_depth_m=depth,
        note="估算值，需按JGJ/T 55或相关规范复核",
    )


